<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Request;
use app\admin\validate\CheckMod;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Config;
class Mod extends Base
{

    protected $mod;
    protected function initialize()
    {
        parent::initialize();
        $this->mod=\app\admin\model\Mod::class;
    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $map = [];

        $query = input('query');
        if ($query) {
            foreach ($query as $vo) {
                $arr = json_decode($vo);
                $map[] = [$arr->column, $arr->condition, $arr->value];
            }
        }

        $dataList = $this->mod::where($map)->order('sort', 'asc')->select();
        return $this->success('获取模型列表成功', $dataList);
    }

    public function appmod(){
        $dbList=$this->mod::field('id,alias,actions,name')->select()->toArray();
        $sysmod=Config::get('app.sysModel.sysmod');
        $dataList=array_merge($dbList,$sysmod);
        return $this->success('己获取应用模型', $dataList);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data = $this->request->param();
        if (isset($data['id'])) {
            try {
                //验证
                $valCheck = validate(CheckMod::class)->scene('edit')->check($data);
                if ($valCheck !== true) {
                    return $this->error($valCheck);
                }
            } catch (ValidateException $e) {
                return $this->error($e->getError());
            }
            if ($this->mod::update($data)) {

                return $this->success('更新成功');
            } else {
                return $this->error('更新失败');
            }
        } else {
            try {
                //验证
                $valCheck = validate(CheckMod::class)->scene('add')->check($data);
                if ($valCheck !== true) {
                    return $this->error($valCheck);
                }
            } catch (ValidateException $e) {
                $this->error($e->getError());
            }
            if ($this->mod::create($data)) {
                return $this->success('增加成功');
            } else {
                return $this->error('增加失败');
            }
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }
    public function getMod($name = '')
    {
        if ($name) {
            $r = $this->mod::where('name', $name)->find();
            return $this->success('获取模型成功', $r);
        }
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    //字段更新
    public function updateCol(int $id,  $modcolumn)
    {
        if ($this->request->isPost()) {
            $r = $this->mod::find($id);
            $r->modcolumn = json_encode($modcolumn);
            $r->save();
            Cache::clear();
            return $this->success('字段更新成功', $r);
            
        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = $this->request->param('id/d');
        if (!$id) {
            $this->error('参数错误');
        } else {
            if ($this->mod::destroy($id)) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        }
    }
}
